/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.synjones.pay.model.context;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 *
 * @author hsd
 */
public class OrderIdGenerator {

    private static long orderId = 1000000000;
    private static String jugeDate = "20170101000000";
    private final static Object _lock = new Object();

    /**
     * *
     *
     * @param sysCode 此参数不同系统使用的字符串不能相同，防止不通系统之间订单重复。
     * sysCode建议在5个字母以内，不要使用特殊符号“_”,"*"等
     * @return
     */
    public static String nextId(String sysCode) {
        if (null == sysCode) {
            sysCode = "";
        }
        String currDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("uuuuMMddHHmmss"));
        synchronized (_lock) {
                if (jugeDate.equals(currDate)) {
                    orderId++;
                } else {
                    jugeDate = currDate;
                    orderId = 1000000000;
                }
            return new StringBuilder(sysCode).append(currDate).append(orderId).toString();
        }

    }
   
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println("date:" + nextId(""));
        }
    }

}
